package com.kingyea.app.market.supplier.controller;

import com.kingyea.app.market.market.bean.Market;
import com.kingyea.app.market.supplier.bean.Supplier;
import com.kingyea.app.market.supplier.service.SupplierService;
import com.kingyea.app.market.supplierAllocation.bean.SupplierAllocation;
import com.kingyea.app.market.supplierAllocation.service.SupplierAllocationService;
import com.kingyea.app.market.utils.page.PageInfo;
import com.kingyea.mobilepolice.system.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description Supplier 控制层
 * @date Created in 2018-02-09 17:47:43
 */
@RequestMapping(value = "/supplier")
@RestController
public class SupplierController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierController.class);

    @Autowired
    private SupplierService supplierService;
    @Autowired
    private SupplierAllocationService supplierAllocationService;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Object list() throws Exception {
        return supplierService.list(null);
    }

    /**
     * 通过ID查找对象
     *
     * @param supplierCode
     * @return
     */
    @RequestMapping(value = "/{supplierCode}", method = RequestMethod.GET)
    public Supplier detail(@PathVariable Integer supplierCode) throws Exception {
        return supplierService.getById(supplierCode);
    }

    /**
     * 根据条件查询
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/condition", method = RequestMethod.POST)
    public Object conditionQuery(@RequestBody Map<String, Object> supplier) throws Exception {
        supplier.put("lxr", supplier.get("supplierName"));
        return supplierService.list(supplier);
    }

    /**
     * 创建
     *
     * @param supplier
     * @return
     */
    @RequestMapping(value = "/addSupplier", method = RequestMethod.POST)
    public Object create(@RequestBody Map<String, Object> supplier) throws Exception {
        //判断是否已经存在
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("_supplierName", supplier.get("supplierName"));
        map.put("companyCode", supplier.get("companyCode"));
        PageInfo re = (PageInfo) supplierService.list(map);
        List<Market> list = re.getList();
        if (list.size() > 0) {
            Result results = new Result();
            results.setCode("B100001");
            return results;
        }
        return supplierService.save(supplier);
    }

    /**
     * 更新
     *
     * @param supplier
     * @return
     */
    @RequestMapping(value = "/editSupplier", method = RequestMethod.POST)
    public Object update(@RequestBody Supplier supplier) throws Exception {
        return supplierService.update(supplier);
    }

    /**
     * 删除
     *
     * @param supplier
     * @return
     */
    @RequestMapping(value = "/deleteSupplier", method = RequestMethod.POST)
    public Object delete(@RequestBody Supplier supplier) throws Exception {
        Map<String, Object> objMap = new HashMap<String, Object>();
        objMap.put("supplierCode", supplier.getSupplierCode());
        PageInfo list = (PageInfo) supplierAllocationService.list(objMap);
        List data = list.getList();
        if (data.size() == 0) {
            return supplierService.deleteById(supplier.getSupplierCode());
        } else {
            Result results = new Result();
            results.setCode("B100001");
            return results;
        }
    }

    /**
     * 删除未使用供应商
     *
     * @param supplierCode
     * @return
     */
    public int deleteSupplier(Integer supplierCode) {
        try {

            List<SupplierAllocation> bySupplierCode = supplierAllocationService.getBySupplierCode(supplierCode);
            if (bySupplierCode.size() == 0) {//关系表没有数据，删除
                supplierService.deleteById(supplierCode);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return 0;

    }

    /**
     * 关联查询  找出供应商已有权限的供应商和未有权限的供应商
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getListBySupplier", method = RequestMethod.POST)
    public Object getListBySupplier(@RequestBody Map<String, Object> obj) throws Exception {
        return supplierService.getListBySupplier(obj);
    }
}
